﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TraceInspector.Data.Server;
using TraceInspector.Model;
using System.Collections.ObjectModel;

namespace TraceInspector.Application
{
    class InspectionTranslator : IEntityTranslator<Model.Inspection,Data.Server.inspect>
    {
        internal static IEntityTranslator<Model.Inspection, Data.Server.inspect> _instance;

        public static IEntityTranslator<Model.Inspection, Data.Server.inspect> Instance
        {
            get { return _instance ?? (_instance = new InspectionTranslator()); }
        }

        public Model.Inspection CreateModel(TraceInspector.Data.Server.inspect dto)
        {
            return UpdateModel(new Model.Inspection(), dto);
        }

        public Model.Inspection UpdateModel(Model.Inspection model, Data.Server.inspect dto)
        {
            if (model.Id != dto.id)
            {
                model.Id = dto.id;
            }
            if (model.DesignPressure != dto.design_pressure)
            {
                model.DesignPressure = (int)dto.design_pressure;
            }
            if (model.Diam != dto.diam)
            {
                model.Diam = dto.diam;
            }
            if (model.InspectionDate != dto.insp_date)
            {
                model.InspectionDate = dto.insp_date;
            }
            if (model.MgConstrDate != dto.mg_construction_date)
            {
                model.MgConstrDate = dto.mg_construction_date;
            }
            if (model.Misc != dto.misc)
            {
                model.Misc = dto.misc;
            }
            if (model.State != dto.state)
            {
                model.State = dto.state;
            }
            if (model.WorkPressure != dto.work_pressure)
            {
                model.WorkPressure = dto.work_pressure;
            }
            if (dto.dct_mg != null)
            {
                model.InspectionMg = GetMgFromDto(dto);
            }

            if (dto.dct_place != null)
            {
                model.InspectionPlace = GetPlaceFromDto(dto);
            }

            if (dto.propusk != null)
            {
                model.Launches = GetLaunchesFromDto(dto);
            }

            return model;
        }

        private static Model.MG GetMgFromDto(inspect dto)
        {
            return new MG()
            {
                Id = dto.dct_mg.id,
                Name = dto.dct_mg.name
            };
        }

        private static Model.Place GetPlaceFromDto(inspect dto)
        {
            return new Place()
            {
                Id = dto.dct_place.id,
                Name = dto.dct_place.name
            };
        }

        private static ObservableCollection<Model.Launch> GetLaunchesFromDto(inspect dto)
        { 
            IEnumerable<Model.Launch> launches = dto.propusk.Select(lch => new Model.Launch()
            {
                Id = lch.id,
                DataPath = lch.data_path,
                StartDate = lch.start_time,
                EndDate = lch.end_time,
                Tubes = GetTubesFromDto(lch),
                LaunchDevice = GetDevice(lch)
            });
            return new ObservableCollection<Launch>(launches);
        }

        private static Device GetDevice(propusk dto)
        {
            return new Device() { Id = dto.id,Name = dto.dct_pig.name,EnglishName = dto.dct_pig.name_en};
        }

        private static ObservableCollection<Model.Tube> GetTubesFromDto(propusk dto)
        {
            //IEnumerable<Model.Tube> tubes = dto.obj_weld.Select(ts => new Tube() {Distance = ts.dist,Orient1 = ts.orient1, Orient2 = ts.orient2 });
            //IEnumerable<Model.Tube> tubes = Data.Server.obj_weld.
            
            //return new ObservableCollection<Tube>(tubes);
            return null;
        }

        public Data.Server.inspect CreateDto(Model.Inspection model)
        {
            return UpdateDto(new inspect(), model);
        }

        public Data.Server.inspect UpdateDto(Data.Server.inspect dto, Model.Inspection model)
        {
            if (model.Id == dto.id)
            {
                dto.id = model.Id;
            }

            if (model.DesignPressure != dto.design_pressure)
            {
                dto.design_pressure = model.DesignPressure;
            }
            if (model.Diam != dto.diam)
            {
                dto.diam = model.Diam;
            }
            if (model.InspectionDate != dto.insp_date)
            {
                dto.insp_date = model.InspectionDate;
            }
            if (model.MgConstrDate != dto.mg_construction_date)
            {
                dto.mg_construction_date = model.MgConstrDate;
            }
            if (model.Misc != dto.misc)
            {
                dto.misc = model.Misc;
            }
            if (model.State != dto.state)
            {
                dto.state = model.State;
            }
            if (model.WorkPressure != dto.work_pressure)
            {
                dto.work_pressure = model.WorkPressure;
            }   
            
            return dto;
        }
    }
}
